﻿using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using FarseerPhysics.Collision;
using FarseerPhysics.Common;


namespace Spacer
{
    public class ShipSelectionScreen : Activity
    {
		public ShipSelectionScreen( PlayerController controller )
		{
			selector = controller;
		}

        public void Initialize()
        {
        }

        public void Shutdown()
        {
        }

        public void Resumed()
        {
        }

        public void Paused()
        {
        }

        public void Update()
        {
            //calls the UpdateMenu function
            UpdateMenu();
        }

        public void DrawWorld()
        {
        }

        public void DrawOverlay()
        {
			if( IsFinished() )
				return;

            Surface.DrawColor = Color.White;
            if (selection == 0)
                Surface.DrawSprite(Main.Sprites["Fighter"], new Vector2(Surface.Width / 2, Surface.Height / 2), (Surface.Width / Surface.Height) * Main.Sprites["Fighter"].Size, Vector2.Zero, 0f, 0);
            if (selection == 1)
                Surface.DrawSprite(Main.Sprites["FighterMedium"], new Vector2(Surface.Width / 2, Surface.Height / 2), (Surface.Width / Surface.Height) * Main.Sprites["FighterMedium"].Size, Vector2.Zero, 0f, 0);
            if (selection == 2)
                Surface.DrawSprite(Main.Sprites["FighterLarge"], new Vector2(Surface.Width / 2, Surface.Height / 2), (Surface.Width / Surface.Height) * Main.Sprites["FighterLarge"].Size, Vector2.Zero, 0f, 0);

            Surface.DrawColor = Color.Yellow;
            Surface.DrawString("DefaultLarge", "Select Your Ship!", new Vector2(Surface.Width / 3 - 200, 50), Vector2.Zero, 0f);

        }

        public bool IsModal()
        {
            return true;
        }

        public bool IsTransparent()
        {
            return false;
        }

        public bool IsFinished()
        {
            return m_bFinished;
        }

        /*Scrolls up and down through the  using W and S keys.
        Right now when enter is pressed, and New Game is selected, it brings you to the game. Otherwise it does nothing. */
        private void UpdateMenu()
        {

            //Scroll through selections
            if ((Input.IsKeyDown(Keys.Right) && !Input.WasKeyDown(Keys.Right)) ||
                (Input.IsGamePadButtonDown(GamePadButton.Right) && !Input.WasGamePadButtonDown(GamePadButton.Right)))
            {
                selection++;
                if (selection > 2)
                    selection = 0;
            }
            else if ((Input.IsKeyDown(Keys.Left) && !Input.WasKeyDown(Keys.Left)) ||
                     (Input.IsGamePadButtonDown(GamePadButton.Left) && !Input.WasGamePadButtonDown(GamePadButton.Left)))
            {
                selection--;
                if (selection < 0)
                    selection = 2;
            }

            //If selection is 0 and enter is pressed, start the game
            if ((Input.IsKeyDown(Keys.Enter) && !Input.WasKeyDown(Keys.Enter)) ||
                (Input.IsGamePadButtonDown(GamePadButton.A) && !Input.WasGamePadButtonDown(GamePadButton.A)))
            {
                if (selection == 0)
                {
                    selector.CurrentShipType = typeof(Fighter);
                }
                else if (selection == 1)
                {
                    selector.CurrentShipType = typeof(FighterMedium);
                }
                else if (selection == 2)
                {
                    selector.CurrentShipType = typeof(FighterLarge);
                }

                m_bFinished = true;
            }
        }
        

        // ----------

		PlayerController selector;
        bool m_bFinished;
        
        private Texture2D mainMenuSheet { get; set; }
        private Texture2D gameLogo { get; set; }
        private int selection { get; set; }
    }
}
